//
//  ProtocolViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/18.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation
import UIKit

private protocol HasName {
    var name : String {get}
}

private struct XiaoHuang : HasName {
    var name : String
}

private protocol SingSong {
    func takeSongs()
}

private class XiaoLi : SingSong {
    var delegate : SingSong?
    func takeSongs() {
        print("I take a protocol function to sing a song")
    }
}

private class John : SingSong{
    func takeSongs() {
        print("I do nothing")
    }
}

private protocol Calling {
    func callSomeOne(_ number : String)
}

private protocol CellPhoneCall : Calling {
    func callSomeOne(_ number: String,withPhone phone:String)
}

private class Lee : CellPhoneCall {
    
    func callSomeOne(_ number: String) {
        print("call the number " + number)
    }
    func callSomeOne(_ number: String, withPhone phone: String) {
        print("call the number " + number + " with " + phone)
    }
}

private protocol Named {
    var name: String { get }
}

private protocol Aged {
    var age: Int { get }
}

private struct Person: Named, Aged {
    var name: String
    var age: Int
}

@objc private protocol Talk {
    func shout()
    @objc optional func whisper()
}

private class Lucy : Talk {
    @objc func shout() {
        print("Lucy just shouting")
    }
}

class ProtocolViewController : BaseViewController,SingSong {
    
    fileprivate var delegate : CellPhoneCall?
    fileprivate var talkDelegate : Talk?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titles = ["properties","methods","delegation","inherit","compose","check","optionalProtocol"]
        self.tableView?.reloadData()
        
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        switch indexPath.row{
        case 0:
            properties()
        case 1:
            methods()
        case 2:
            delegation()
        case 3:
            inherit()
        case 4:
            compose()
        case 5:
            check()
        case 6:
            optionalProtocol()
        default:
            print("no more methods")
        }
    }
    
    func takeSongs() {
        print("what? viewController take the protocol method???")
    }
    
    fileprivate func properties(){
        let xiaohuang = XiaoHuang.init(name: "xiao huang")
        print(xiaohuang)
    }
    
    fileprivate func methods(){
        let xiaoli = XiaoLi()
        xiaoli.takeSongs()
    }
    
    fileprivate func delegation(){
        let xiaoli = XiaoLi()
        xiaoli.delegate = self
        xiaoli.delegate?.takeSongs()
    }
    
    fileprivate func inherit(){
        let lee = Lee()
        self.delegate = lee
        self.delegate?.callSomeOne("10086", withPhone: "iphone6S")
    }
    
    fileprivate func compose(){
        func wishHappyBirthday(_ celebrator: Named & Aged) {
            print("Happy birthday \(celebrator.name) - you're \(celebrator.age)!")
        }
        let birthdayPerson = Person(name: "Malcolm", age: 21)
        wishHappyBirthday(birthdayPerson)
    }
    
    fileprivate func check(){
        let xiaoLi = XiaoLi()
        let lee = Lee()
        let john = John()
        
        let array = [xiaoLi,lee,john] as [Any]
        for item in array {
            if ((item as? SingSong) != nil) {
                print(String(describing: item) + " has singSong protocol")
            }else if item as? Calling != nil {
                print(String(describing: item) + " has calling protocol")
            }
        }
    }
    
    fileprivate func optionalProtocol(){
        let lucy = Lucy()
        self.talkDelegate = lucy
        self.talkDelegate?.shout()
    }
}
